package utils

import (
	"reflect"
	"regexp"
)

var specialChars = regexp.MustCompile(`[\s]+`)

// CleanStruct 递归清理结构体中的字符串字段
func CleanStruct(v interface{}) {
	//val := reflect.ValueOf(v)
	//if val.Kind() == reflect.Ptr {
	//	val = val.Elem()
	//}
	//cleanValue(val)
}
func cleanValue(v reflect.Value) {
	switch v.Kind() {
	case reflect.Struct:
		for i := 0; i < v.NumField(); i++ {
			field := v.Field(i)
			if field.CanSet() {
				cleanValue(field)
			}
		}
	case reflect.String:
		if v.CanSet() {
			cleaned := specialChars.ReplaceAllString(v.String(), "")
			v.SetString(cleaned)
		}
	case reflect.Slice, reflect.Array:
		for i := 0; i < v.Len(); i++ {
			cleanValue(v.Index(i))
		}
	case reflect.Map:
		for _, key := range v.MapKeys() {
			value := v.MapIndex(key)
			cleanValue(value)
		}
	case reflect.Ptr, reflect.Interface:
		if !v.IsNil() {
			cleanValue(v.Elem())
		}
	}
}
